﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kugar.Core.Security
{
    /// <summary>
    ///     根据传入的用户ID,检查该用户是否具有特定的权限
    /// </summary>
    public interface IPermissionCheckProvider:IDisposable
    {
        int CheckPermissionByID(int userID,int permissionID);
        int CheckPermissionByName(int userID,string permissionName);

        KeyValuePair<int, int>[] CheckPermissionByIDList(int userID, int[] permissionIDList);

        /// <summary>
        ///     返回指定名称的列表中,所有
        /// </summary>
        /// <param name="useriD"></param>
        /// <param name="permissionNameList"></param>
        /// <returns></returns>
        KeyValuePair<string, int>[] CheckPermissionByNameList(int useriD, string[] permissionNameList);

        bool HasPermission(int useriD, string permissionName);
        bool HasPermission(int useriD, string[] permissionNameList);

        bool HasPermission(int useriD, int permissionID);
        bool HasPermission(int useriD, int[] permissionIDList);

        RMS_PermissionValueCollection GetPermissionListByUserID(int userID);

        /// <summary>
        ///     返回所有的权限子项
        /// </summary>
        /// <returns></returns>
        RMS_PermissionItem[] GetPermissionItemList();

        /// <summary>
        ///     获取权限分类信息
        /// </summary>
        /// <returns></returns>
        RMS_PermissionCategory[] GetPermissionCategroy();

    }

    public abstract class PermissionCheckProviderBase : IPermissionCheckProvider
    {
        #region Implementation of IDisposable

        public virtual void Dispose()
        {
        }

        #endregion

        #region Implementation of IPermissionProvider

        public abstract int CheckPermissionByID(int useriD, int permissionID);
        public abstract int CheckPermissionByName(int useriD, string permissionName);
        public abstract KeyValuePair<int, int>[] CheckPermissionByIDList(int useriD, int[] permissionIDList);
        public abstract KeyValuePair<string, int>[] CheckPermissionByNameList(int useriD, string[] permissionNameList);

        public virtual bool HasPermission(int useriD, string permissionName)
        {
            return CheckPermissionByName(useriD, permissionName) > 0;
        }

        public virtual bool HasPermission(int useriD, string[] permissionNameList)
        {
            return !CheckPermissionByNameList(useriD, permissionNameList).Where((k) => k.Value < 0).Any();
        }

        public virtual bool HasPermission(int useriD, int permissionID)
        {
            return CheckPermissionByID(useriD, permissionID) > 0;
        }

        public virtual bool HasPermission(int useriD, int[] permissionIDList)
        {
            return !CheckPermissionByIDList(useriD, permissionIDList).Where((k) => k.Value < 0).Any();
        }


        public RMS_PermissionItem[] GetPermissionItemList()
        {
            throw new NotImplementedException();
        }

        public RMS_PermissionCategory[] GetPermissionCategroy()
        {
            throw new NotImplementedException();
        }

        public abstract RMS_PermissionValueCollection GetPermissionListByUserID(int useriD);

        #endregion
    }
}
